var pool = require("../conf/db").pool;
var logger = require('../conf/log4').logger('dao');

function saveRank(data) {
    var sql = "insert into t_az"+data.source+"_pro_ranking(fetch_time,asin,page_num,page_set,category_name,keyword,";
    sql += "img_url,rank,pro_price,pro_grade,comment_total_number,pro_name,is_promotion_price,brand_name)";
    sql += "values(CURDATE(),?,?,?,?,? ,?,?,?,?,?,?,?,?)";
    pool.getConnection(function (err, connection) {
        if(err){
            logger.error(err);
            saveRank(data);
        } else{
            connection.query(sql,
                [data.asin, data.page, data.index, data.category, data.keyword, data.img, data.rank, data.price, data.grade || 0,
                    data.comm || 0, data.title, data.is_pro_price, data.brand],
                function (err, rows) {
                    if (err) {
                        logger.error(data);
                        logger.error(err);
                        //console.log(data);
                        //console.log(err);
                    }
                    connection.release();
                });
        }
    });
};


function saveAbsrRank(data) {
    var sql = "insert into t_az"+data.source+"_absr_ranking(fetch_time,asin,pro_price,rank,category_name,pro_url,pro_grade," +
        "comment_total_number)VALUES(CURDATE(),?,?,?,?,?,?,?)";
    pool.getConnection(function (err, connection) {
        if(err) logger.error(err);
        connection.query(sql,
            [data.asin, data.price, data.rank, data.category, data.pro_url, data.grade || 0, data.comm || 0],
            function (err, rows) {
                if (err) {
                    logger.error(data);
                    logger.error(err);
                }
                connection.release();
            });
    });
};


function saveAbsrInfo(data) {
    var sql = "insert into t_az"+data.source+"_absr_info(fetch_time,asin,absr_rank,absr_url,category_path)values(CURDATE(),?,?,?,?)" ;
    pool.getConnection(function (err, connection) {
        if(err) logger.error(err);
        connection.query(sql,
            [data.asin, data.absr_rank, data.absr_url, data.category_path],
            function (err, rows) {
                if (err) {
                    logger.error(data);
                    logger.error(err);
                }
                connection.release();
            });
    });
};

function savePro(data) {
    var sql = "insert into t_az"+data.source+"_pro(fetch_time,asin,pro_name,pro_price,brand_name,pro_img_urls,category_path,absr_info,description,pro_grade,comment_total_number," +
        "comment_level_five,comment_level_four,comment_level_three,comment_level_two,comment_level_one)values(CURDATE(),?,?,?,?,?,?,?,?,?,? ,?,?,?,?,?)";
    pool.getConnection(function (err, connection) {
        if(err) logger.error(err);
        connection.query(sql,
            [data.asin, data.pro_name, data.price, data.brand, data.imgs, data.category, data.absr, data.desc, data.grade || 0, data.comm || 0,
                data.star_5, data.star_4, data.star_3, data.star_2, data.star_1],
            function (err, rows) {
                if (err) {
                    clogger.error(data);
                    logger.error(err);
                }
                connection.release();
            });
    });
}
function saveProMHRL(data) {
    var sql = "insert into t_az"+data.source+"_h_comment_detail(fetch_time,comment_create_date,asin,comment_name,pro_color,cust_url,comment_grade" +
        ",comment_content,title,comment_url,original_date,url_asin,review_id) values(CURDATE(),?,?,?,?,?,?, ?,?,?,?,?,?)";
        pool.getConnection(function (err, connection) {
            if(err) logger.error(err);
            connection.query(sql,
                [data.comm_create,data.asin, data.comm_name, data.comm_color, data.cust_url, data.comm_grade||0, data.comment,
                    data.comm_title, data.comm_url, data.comm_date, data.asin, data.reviewId],
                function (err, rows) {
                    if (err) {
                        logger.error(data);
                        logger.error(err);
                    }
                    connection.release();
                });

        });
}

function saveProCMCR(data) {
    var sql = "replace into t_az"+data.source+"_r_comment_detail(fetch_time,comment_create_date,asin,comment_name,pro_color,cust_url,comment_grade" +
        ",comment_content,title,comment_url,original_date,url_asin,review_id,comment_page,comment_page_set) values(CURDATE(),?,?,?,?,?,?, ?,?,?,?,?,?,?,?)";
    pool.getConnection(function (err, connection) {
        if(err) logger.error(err);
        connection.query(sql,
            [data.comm_create,data.asin, data.comm_name, data.comm_color, data.cust_url, data.comm_grade, data.comment,
                data.comm_title, data.comm_url, data.comm_date, data.asin, data.reviewId,data.page,data.page_set],
            function (err, rows) {
                if (err) {
                    logger.error(data);
                    logger.error(err);
                }
                connection.release();
            });

    });
}

function saveBeBound(data) {
    var sql = "insert into t_az"+data.source+"_be_bound_warning(fetch_time,url_asin,bound_color,bound_asin) VALUES(CURDATE(),?,?,?)";
    pool.getConnection(function (err, connection) {
        if(err) logger.error(err);
        connection.query(sql,
            [data.asin,data.bound_color, data.bound_asin ],
            function (err, rows) {
                if (err) {
                    logger.error(data);
                    logger.error(err);
                }
                connection.release();
            });
    });
}

function findKeyword(source,foo) {
    var sql = "select category,keyword from t_base_url where source='"+source+"' and (type='TYPE_RANK' or type='TYPE_RANK_2') ";
    pool.getConnection(function (err, connection) {
        if(err) console.log(err);
        connection.query(sql, function (err, rows, fields) {
            if (err) {
                console.log('[query] - :' + err);
                return;
            }
            foo(rows);
        });
    });
};

function findAbsrCategory(source,foo) {
    var sql = "select url,category from t_base_url where source='"+source+"' and type='TYPE_ABSR_RANK'";
    //console.log(sql);
    pool.getConnection(function (err, connection) {
        if(err) console.log(err);
        connection.query(sql, function (err, rows, fields) {
            if (err) {
                console.log('[query] - :' + err);
                return;
            }
            foo(rows);
        });
    });
};

function findProAsin(source,foo,fn) {
    var sql =  "select asin from t_az"+source+"_pro_ranking  a ";
        sql += "left join (select asin from v_esr_pro_asin where source='"+source+"' and is_putaway=1) b using(asin) ";
        sql += "where a.fetch_time=CURDATE() and (a.page_type=0 or a.page_type=1) and a.page_num<=10 group by a.asin";
    //console.log(sql);
    pool.getConnection(function (err, connection) {
        if(err) console.log(err);
        connection.query(sql, function (err, rows, fields) {
            if (err) {
                console.log('[query] - :' + err);
                return;
            }
            foo(rows,fn);
        });
    });
};

module.exports.saveRank = saveRank;
module.exports.saveAbsrRank = saveAbsrRank;
module.exports.savePro = savePro;
module.exports.saveProMHRL = saveProMHRL;
module.exports.saveProCMCR = saveProCMCR;
module.exports.saveBeBound = saveBeBound;
module.exports.saveAbsrInfo = saveAbsrInfo;


module.exports.findKeyword = findKeyword;
module.exports.findAbsrCategory = findAbsrCategory;
module.exports.findProAsin = findProAsin;

